package zhieasy.cn.service.forum.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.forum.ForumCommentRoot;
import zhieasy.cn.model.forum.ForumInfo;
import zhieasy.cn.persistence.mapper.forum.ForumInfoMapper;
import zhieasy.cn.service.forum.ForumCommentRootService;
import zhieasy.cn.service.forum.ForumInfoService;
import zhieasy.cn.service.forum.ForumLikeUserService;

import java.util.List;

/**
 * @author DaHang
 * @date 2020/8/29 16:38
 */
@Service
public class ForumInfoServiceImpl extends ServiceImpl<ForumInfoMapper, ForumInfo> implements ForumInfoService {
    @Autowired
    ForumInfoMapper forumInfoMapper;
    @Autowired
    ForumCommentRootService forumCommentRootService;

    @Autowired
    ForumLikeUserService forumLikeUserService;
    @Override
    public ResultVO getAllForums(int pageIndex, int pageSize , String openId) {
        List<ForumInfo> forumInfos =
            forumInfoMapper.selectForumListByPage(pageSize * (pageIndex - 1), pageSize * pageIndex);
        //这时候要把这里面的数据初始化一下，看这个人到底有没有点过这些赞
        for (ForumInfo forumInfo: forumInfos) {
            forumInfo.setLike(forumLikeUserService.isLikeForum(forumInfo.getId(), openId));
        }
        return ResultUtils.success(forumInfos);
    }

    @Override
    public ResultVO addLike(int id) {
        int i = forumInfoMapper.addLikeOfForum(id);

        if (i == 1) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @Override
    public ResultVO subtractLikeOfForum(int forumId) {
        int i = forumInfoMapper.subtractLikeOfForum(forumId);

        if (i == 1) {
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }

    @Override
    public ResultVO getForumDetail(int forumId) {
        forumInfoMapper.addClickOfForum(forumId);
        ForumInfo forumInfo = forumInfoMapper.selectById(forumId);
        if (forumInfo == null){
            return ResultUtils.error("没有这个帖子");
        }
        //拿一下这个帖子里面的评论，一次拿5个
        List<ForumCommentRoot> commonRootList = forumCommentRootService.getCommonRootByForumId(forumInfo.getId(), 1, 5);
        forumInfo.setForumCommentRootList(commonRootList);
        return ResultUtils.success(forumInfo);
    }

    @Override
    public ResultVO forwardForum(int forumId) {
        int i = forumInfoMapper.addForwardOfForum(forumId);
        if (i == 1){
            return ResultUtils.success();
        }
        return ResultUtils.error("数据交互未成功");
    }


}
